﻿using Microsoft.Extensions.DependencyInjection;
using RuoVea.Entity;
using RuoVea.EventBus;
using RuoVea.ExApp;
using RuoVea.ExSqlSugar;
using RuoVea.ExUtil;
using RuoVea.Util;
using SqlSugar;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;


namespace RuoVea.Service
{
    /// <summary>
    /// 
    /// </summary>
    public class LogEventSubscriber : IEventSubscriber
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="services"></param>
        public LogEventSubscriber(IServiceProvider services)
        {
            Services = services;
        }

        public IServiceProvider Services { get; }

        [EventSubscribe("Create:OpLog")]
        public async Task CreateOpLog(EventHandlerExecutingContext context)
        {
            using var scope = Services.CreateScope();
            var _repository = scope.ServiceProvider.GetRequiredService<ISqlSugarRepository<SysLogOp>>();
            //var log = (SysLogOp)context.Source.Payload;
            var log = context.Source.Payload.ObjToModel<SysLogOp>();
            long userId;
           bool reslut= long.TryParse( App.User?.FindFirst(RuoVea.ExDto.ClaimConst.CLAINM_USERID)?.Value,out userId);
            if(reslut) log.userId = userId;
            await _repository.InsertAsync(log);
        }

        [EventSubscribe("Create:ExLog")]
        public async Task CreateExLog(EventHandlerExecutingContext context)
        {
            using var scope = Services.CreateScope();
            var _repository = scope.ServiceProvider.GetRequiredService<ISqlSugarRepository<SysLogEx>>();
            //var log = (SysLogEx)context.Source.Payload;
            var log = context.Source.Payload.ObjToModel<SysLogEx>();
            long userId;
            bool reslut = long.TryParse(App.User?.FindFirst(RuoVea.ExDto.ClaimConst.CLAINM_USERID)?.Value, out userId);
            if (reslut) log.userId = userId;
            await _repository.InsertAsync(log);
        }

        [EventSubscribe("Create:VisLog")]
        public async Task CreateVisLog(EventHandlerExecutingContext context)
        {
            using var scope = Services.CreateScope();
            var _repository = scope.ServiceProvider.GetRequiredService<ISqlSugarRepository<SysLogVis>>();
            //var log = (SysLogVis)context.Source.Payload;
            var log = context.Source.Payload.ObjToModel<SysLogVis>();
            long userId;
            bool reslut = long.TryParse(App.User?.FindFirst(RuoVea.ExDto.ClaimConst.CLAINM_USERID)?.Value, out userId);
            if (reslut) log.userId = userId;
            await _repository.InsertAsync(log);
        }
        
        [EventSubscribe("Update:UserLoginInfo")]
        public async Task UpdateUserLoginInfo(EventHandlerExecutingContext context)
        {
            using var scope = Services.CreateScope();
            var _repository = scope.ServiceProvider.GetRequiredService<ISqlSugarRepository<SysUser>>();
            var log = (SysUser)context.Source.Payload;

            await _repository.Context.Updateable(log).UpdateColumns(m => new { m.LastLoginTime, m.LastLoginIp })
                .ExecuteCommandAsync();
        }
    }
}
